<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="goods">
        <el-input
          v-model="queryParams.goods"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <div class="goods-container">
      <div
        class="card-wapper"
        v-for="(item,index) in shopList"
        :key="index"
        @click="handleClick(item.url)"
      >
        <div class="iamge">
          <el-image :src="`${realSrc(item.img)}`"/>
        </div>
        <div class="content">
          <div class="price">
            <el-tag size="medium" type="danger">{{ item.goods }}</el-tag>
          </div>
          <span class="desc">{{ item.introduction }}</span>
        </div>
      </div>
    </div>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
      :pageSizes="[12, 24, 48, 100]"
    />

    <!-- 添加或修改商城对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品名称" prop="goods">
          <el-input v-model="form.goods" placeholder="请输入商品名称"/>
        </el-form-item>
        <el-form-item label="图片url" prop="img">
          <image-upload v-model="form.img"/>
        </el-form-item>
        <el-form-item label="商品介绍" prop="Introduction">
          <el-input v-model="form.Introduction" placeholder="请输入商品介绍"/>
        </el-form-item>
        <el-form-item label="商城url" prop="url">
          <el-input v-model="form.url" placeholder="请输入商城url"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listShop, getShop, delShop, addShop, updateShop} from "@/api/business/shop";
import {isExternal} from "@/utils/validate";

export default {
  name: "Shop",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商城表格数据
      shopList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 12,
        goods: null,
        img: null,
        Introduction: null,
        url: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    realSrc(url) {
      return process.env.VUE_APP_BASE_API + url;
    },
    handleClick(url) {
      // let path=window.location.protocol+'//'+url
      window.open(url, "_blank")
    },
    /** 查询商城列表 */
    getList() {
      this.loading = true;
      listShop(this.queryParams).then(response => {
        console.log(response, 'response')
        this.shopList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        goods: null,
        img: null,
        Introduction: null,
        url: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商城";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getShop(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商城";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateShop(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addShop(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商城编号为"' + ids + '"的数据项？').then(function () {
        return delShop(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/shop/export', {
        ...this.queryParams
      }, `shop_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.goods-container {
  display: flex;
  flex-wrap: wrap;
  min-height: calc(100vh - 400px);
}

.goods-container:after {
  flex: auto;
}

.card-wapper {
  height: 320px;
  position: relative;
  display: flex;
  flex: 1;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: calc((100% - 20px) / 7);
  max-width: calc((100% - 20px) / 7);
  padding: 20px 8px;
  margin: 10px;
  cursor: pointer;
  border: 1px solid #e4e7ed;
  border-radius: 8px;
}

.card-wapper:hover {
  box-shadow: 0 0px 10px #e4ecf8;
  transition: all 0.5s ease-out;
}

.iamge {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 180px;
  height: 180px;
  margin-bottom: 15px;
}

.content {
  display: flex;
  flex-direction: column;
  width: 100%;
}

.title {
  min-height: 16px;
  margin-top: 10px;
  margin-bottom: 6px;
  overflow: hidden;
  font-size: 13px;
  font-weight: bold;
  line-height: 16px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.desc {
  min-height: 16px;
  overflow: hidden;
  font-size: 13px;
  line-height: 16px;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-top: 12px;
}

</style>
